#! /bin/bash

# (c) Michael Goetze, 2011, mgoetze@mgoetze.net
# (c) Thomas Lange 2014

error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code
if ifclass CDH5_EL7 ; then

#install Oracle JDK8
#$ROOTCMD cd /root
#$ROOTCMD wget http://faiserver/jdk-8u161-linux-x64.rpm
#$ROOTCMD rpm -i jdk-8u161-linux-x64.rpm
#jdbc mysql 5.6 need >= 5.1.38
#install depend only
#$ROOTCMD yum install -y mysql-connector-java && $ROOTCMD yum remove -y mysql-connector-java
$ROOTCMD wget http://faiserver/mysql-connector-java-5.1.46-bin.jar
$ROOTCMD mkdir /usr/share/java
$ROOTCMD cp -f mysql-connector-java-5.1.46-bin.jar /usr/share/java/mysql-connector-java.jar

#cat << 'EOF' > $target/etc/profile.d/java.sh
#set java environment
#JAVA_HOME=/usr/java/jdk1.8.0_161
#JRE_HOME=$JAVA_HOME/jre
#PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
#CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
#export JAVA_HOME JRE_HOME PATH CLASSPATH
#EOF

#enable CDH5 repo
sed -i 's/^enabled=.*$/enabled=1/' $target/etc/yum.repos.d/cloudera-manager.repo
sed -i 's/^enabled=.*$/enabled=1/' $target/etc/yum.repos.d/cloudera-cdh5.repo

#Install Cloudera Manager Agent Packages
$ROOTCMD yum install -y oracle-j2sdk1.7 cloudera-manager-agent cloudera-manager-daemons

#disable transparent_hugepage 
ainsl /etc/rc.local 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'
ainsl /etc/rc.local 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'

fi

if ifclass HDP_EL7 ; then

#install Oracle JDK8
$ROOTCMD cd /root
$ROOTCMD wget http://faiserver/jdk-8u161-linux-x64.rpm
$ROOTCMD rpm -i jdk-8u161-linux-x64.rpm
cat << 'EOF' > $target/etc/profile.d/java.sh

#set java environment
JAVA_HOME=/usr/java/jdk1.8.0_161
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
EOF

#enable HDP repo
sed -i 's/^enabled=.*$/enabled=1/' $target/etc/yum.repos.d/ambari.repo
sed -i 's/^enabled=.*$/enabled=1/' $target/etc/yum.repos.d/hdp.repo

fi
exit $error
